import os
from datetime import datetime
from fastapi import APIRouter, Depends, File, Form, Query, Request, UploadFile
from sqlalchemy.ext.asyncio import AsyncSession
from typing import Literal, Optional, Union
from pydantic_validation_decorator import ValidateFields
from config.get_db import get_db
from config.enums import BusinessType
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.machine_vo import MachineModel,MachinePageQueryModel,DeleteMachineModel
from module_admin.service.machine_service import MachineService
from module_admin.service.login_service import LoginService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil
from module_admin.entity.vo.user_vo import CurrentUserModel

machineController = APIRouter(prefix='/game/machine', dependencies=[Depends(LoginService.get_current_user)])

@machineController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('game:machine:list'))]
)
async def get_game_machine_list(
    request: Request,
    machine_page_query: MachinePageQueryModel = Depends(MachinePageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('BaMachine')),
):
    
    machine_page_query_result = await MachineService.get_list_services(
        query_db, machine_page_query, data_scope_sql, is_page=True
    )

    return ResponseUtil.success(model_content=machine_page_query_result)

@machineController.get(
    '/{id}', response_model=MachineModel, dependencies=[Depends(CheckUserInterfaceAuth('game:machine:query'))]
)
async def query_detail_machine(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    detail_result = await MachineService.detail_services(query_db, id)

    return ResponseUtil.success(data=detail_result)

@machineController.post('', dependencies=[Depends(CheckUserInterfaceAuth('game:machine:add'))])
@ValidateFields(validate_model='add_validate_model')
@Log(title='机台管理', business_type=BusinessType.INSERT)
async def add_machine(
    request: Request,
    add_validate_model: MachineModel,
    query_db: AsyncSession = Depends(get_db)
):
    add_validate_model.create_time = int(datetime.now().timestamp())
    add_validate_model.update_time = int(datetime.now().timestamp())
    add_dict_type_result = await MachineService.add_services(request, query_db, add_validate_model)
    logger.info(add_dict_type_result.message)

    return ResponseUtil.success(msg=add_dict_type_result.message)


@machineController.put('', dependencies=[Depends(CheckUserInterfaceAuth('game:machine:edit'))])
@ValidateFields(validate_model='edit_validate_model')
@Log(title='机台管理', business_type=BusinessType.UPDATE)
async def edit_machine(
    request: Request,
    edit_validate_model: MachineModel,
    query_db: AsyncSession = Depends(get_db),
):
    # logger.info(f'edit_validate_model: {edit_validate_model}')
    edit_validate_model.update_time = int(datetime.now().timestamp())
    edit_result = await MachineService.edit_services(request, query_db, edit_validate_model)
    logger.info(edit_result.message)

    return ResponseUtil.success(msg=edit_result.message)

@machineController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('game:machine:remove'))])
@Log(title='机台管理', business_type=BusinessType.DELETE)
async def delete_system_dict_type(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_result = await MachineService.delete_services(request, query_db, ids)
    logger.info(delete_result.message)

    return ResponseUtil.success(msg=delete_result.message)

@machineController.put('/changeStatus', dependencies=[Depends(CheckUserInterfaceAuth('game:machine:edit'))])
@Log(title='机台管理', business_type=BusinessType.UPDATE)
async def change_machine_status(
    request: Request,
    change_Model: MachineModel,
    query_db: AsyncSession = Depends(get_db),
):
    logger.info(f'changeStatus:')
    edit_model = MachineModel(
        id=change_Model.id,
        status=change_Model.status,
        update_time=int(datetime.now().timestamp()),
    )
    logger.info(f'edit_model: {edit_model}')
    edit_result = await MachineService.edit_services(request, query_db, edit_model)
    logger.info(edit_result.message)

    return ResponseUtil.success(msg=edit_result.message)

@machineController.get('/machineSelect', dependencies=[Depends(CheckUserInterfaceAuth('game:machine:list'))])
async def get_machine_select(
    request: Request, query_db: AsyncSession = Depends(get_db), data_scope_sql: str = Depends(GetDataScope('BaMachine'))
):
    search_list_query_result = await MachineService.get_machine_select_services(query_db, MachineModel(**{}), data_scope_sql)
    logger.info(f'search_list_query_result: {search_list_query_result}')

    return ResponseUtil.success(model_content=search_list_query_result)